package pers.wwz.study.tencentcop.task;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import pers.wwz.study.tencentcop.controller.SendMessageController;
import pers.wwz.study.tencentcop.utils.DateUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 定时任务。推送健康营养饮食建议
 */
@Component
@Slf4j
@EnableScheduling
@Data
public class SendMealSuggestTask {

    private Integer index = 0;

    @Autowired
    private SendMessageController sendMessageController;

    private static final HashMap<String, List> BREAKFAST_SUGGEST = new HashMap<>(10);;

    private static final HashMap<String, List> LANUCH_SUGGEST = new HashMap<>(10);

    private static final HashMap<String, List> DINNER_SUGGEST = new HashMap<>(10);;



    /**
     * 早餐建议
     */
    @Scheduled(cron="0 0/10 7 * * ?")
    @Async("myThreadPoolTaskExecutor")
    public void breafastSuggest() throws Exception {
        List list = BREAKFAST_SUGGEST.get(DateUtils.getWeekDayString());
        if(!CollectionUtils.isEmpty(list)){
            sendMessageController.senMsg(list.get(this.getIndex()).toString());
            log.info("发送早餐建议成功");
        }
    }

    /**
     * 午餐建议
     */
    @Scheduled(cron="0 0/10 10-11 * * ?")
    @Async("myThreadPoolTaskExecutor")
    public void lanuchSuggest() throws Exception {
        List list = LANUCH_SUGGEST.get(DateUtils.getWeekDayString());
        if(!CollectionUtils.isEmpty(list)){
            sendMessageController.senMsg(list.get(this.getIndex()).toString());
            log.info("发送午餐建议成功");

        }
    }

    /**
     * 晚餐建议
     * 15-16点每10分钟提醒一次
     *
     */
    @Scheduled(cron="0 0/10 15-16 * * ?")
    @Async("myThreadPoolTaskExecutor")
    public void dinnerSuggest() throws Exception {
        List list = DINNER_SUGGEST.get(DateUtils.getWeekDayString());
        if(!CollectionUtils.isEmpty(list)){
            sendMessageController.senMsg(list.get(this.getIndex()).toString());
            log.info("发送晚餐建议成功");

        }
    }

    /**
     * 更改取值索引
     * 每周日凌晨一点:0 0 1 ? * 1
     */
    @Scheduled(cron="0 0 1 ? * 1")
    @Async("myThreadPoolTaskExecutor")
    public void changeIndex() throws Exception {
        if(this.getIndex()==0){
            this.setIndex(1);
        }else {
            this.setIndex(0);
        }
        log.info("更改取值索引成功");

    }


    /**
     * 自定义线程池
     *
     * 添加异步线程时 更新CorePoolSize +1
     */
    @Bean(name = "myThreadPoolTaskExecutor")
    public TaskExecutor getMyThreadPoolTaskExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(2);
        taskExecutor.setMaxPoolSize(200);
        taskExecutor.setQueueCapacity(25);
        taskExecutor.setKeepAliveSeconds(200);
        taskExecutor.setThreadNamePrefix("健康均衡饮食推荐系统");
        // 线程池对拒绝任务（无线程可用）的处理策略，目前只支持AbortPolicy、CallerRunsPolicy；默认为后者
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //调度器shutdown被调用时等待当前被调度的任务完成
        taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        //等待时长
        taskExecutor.setAwaitTerminationSeconds(60);
        taskExecutor.initialize();
        return taskExecutor;
    }

    static {


        // 周一早餐
        List breakFastSuggestList1 = new ArrayList<>();
        breakFastSuggestList1.add(
                "今天早餐建议食谱:\n" +
                        "1.赤豆二米粥\n" +
                        "2.椒盐卷\n" +
                        "3.醋溜西葫芦\n" +
                        "4.水煮蛋");
        breakFastSuggestList1.add(
                "今天早餐建议食谱:\n" +
                        "1.红糖粳米粥\n" +
                        "2.校友卷\n" +
                        "3.儿童蒸蛋\n" +
                        "4.醋溜西葫芦");
        BREAKFAST_SUGGEST.put("星期一",breakFastSuggestList1);

        // 周二早餐
        List breakFastSuggestList2 = new ArrayList<>();
        breakFastSuggestList2.add(
                "今天早餐建议食谱:\n" +
                        "1.玉米棒碴粥\n" +
                        "2.南瓜馒头\n" +
                        "3.黄瓜炒鸡蛋\n" +
                        "4.蚝油生菜");
        breakFastSuggestList2.add(
                "今天早餐建议食谱:\n" +
                        "1.小米薏仁粥\n" +
                        "2.蔬菜煎饼\n" +
                        "3.五香鹌鹑蛋\n" +
                        "4.西芹炒香干");

        BREAKFAST_SUGGEST.put("星期二",breakFastSuggestList2);

        // 周三早餐
        List breakFastSuggestList3 = new ArrayList<>();
        breakFastSuggestList3.add(
                "今天早餐建议食谱:\n" +
                        "1.暖心南瓜粥\n" +
                        "2.黑米面馒头\n" +
                        "3.鸡丝小人参\n" +
                        "4.五香鹌鹑蛋");
        breakFastSuggestList3.add(
                "今天早餐建议食谱:\n" +
                        "1.黄金粥\n" +
                        "2.手工杠子馍\n" +
                        "3.卤鸡肝\n" +
                        "4.番茄炒鸡蛋");
        BREAKFAST_SUGGEST.put("星期三",breakFastSuggestList3);

        // 周四早餐
        List breakFastSuggestList4 = new ArrayList<>();
        breakFastSuggestList4.add(
                "今天早餐建议食谱:\n" +
                        "1.藜麦胚芽粥\n" +
                        "2.双色卷\n" +
                        "3.水煮柴鸡蛋\n" +
                        "4.蒜香油麦菜");
        breakFastSuggestList4.add(
                "今天早餐建议食谱:\n" +
                        "1.香菇鸡肉粥\n" +
                        "2.红糖卷\n" +
                        "3.丝瓜炒蛋\n" +
                        "4.卤鸡肝");
        BREAKFAST_SUGGEST.put("星期四",breakFastSuggestList4);

        // 周五早餐
        List breakFastSuggestList5 = new ArrayList<>();
        breakFastSuggestList5.add(
                "今天早餐建议食谱:\n" +
                        "1.大米薏仁粥\n" +
                        "2.枣香馒头\n" +
                        "3.肉蓉线茄\n" +
                        "4.麻油蒸蛋");
        breakFastSuggestList5.add(
                "今天早餐建议食谱:\n" +
                        "1.南瓜小米粥\n" +
                        "2.芝麻馒头\n" +
                        "3.黄瓜炒鸡蛋\n" +
                        "4.鸡丝小人参");
        BREAKFAST_SUGGEST.put("星期五",breakFastSuggestList5);


        // 周六早餐
        List breakFastSuggestList6 = new ArrayList<>();
        breakFastSuggestList6.add(
                "今天早餐建议食谱:\n" +
                        "1.暖心南瓜粥\n" +
                        "2.黑米面馒头\n" +
                        "3.鸡丝小人参\n" +
                        "4.五香鹌鹑蛋");
        breakFastSuggestList6.add(
                "今天早餐建议食谱:\n" +
                        "1.黄金粥\n" +
                        "2.手工杠子馍\n" +
                        "3.卤鸡肝\n" +
                        "4.番茄炒鸡蛋");
        BREAKFAST_SUGGEST.put("星期六",breakFastSuggestList6);

        // 周日早餐
        List breakFastSuggestList7 = new ArrayList<>();
        breakFastSuggestList7.add(
                "今天早餐建议食谱:\n" +
                        "1.藜麦胚芽粥\n" +
                        "2.双色卷\n" +
                        "3.水煮柴鸡蛋\n" +
                        "4.蒜香油麦菜");
        breakFastSuggestList7.add(
                "今天早餐建议食谱:\n" +
                        "1.香菇鸡肉粥\n" +
                        "2.红糖卷\n" +
                        "3.丝瓜炒蛋\n" +
                        "4.卤鸡肝");
        BREAKFAST_SUGGEST.put("星期日",breakFastSuggestList7);



        // 午餐

        // 周一午餐
        List lanuchSuggestList1 = new ArrayList<>();
        lanuchSuggestList1.add(
                "今天午餐建议食谱:\n" +
                        "1.黑芝麻米饭\n" +
                        "2.豉汁蒸排骨\n" +
                        "3.干锅千页豆腐\n" +
                        "4.酸梅汤");
        lanuchSuggestList1.add(
                "今天午餐建议食谱:\n" +
                        "1.香米饭\n" +
                        "2.双菇炖鸡块\n" +
                        "3.麻酱菠菜\n" +
                        "4.五果汤");
        LANUCH_SUGGEST.put("星期一",lanuchSuggestList1);

        // 周二午餐
        List lanuchSuggestList2 = new ArrayList<>();
        lanuchSuggestList2.add(
                "今天午餐建议食谱:\n" +
                        "1.时蔬鸡汤面\n" +
                        "2.白灼大虾\n" +
                        "3.芝麻小烧饼");
        lanuchSuggestList2.add(
                "今天午餐建议食谱:\n" +
                        "1.炝锅面\n" +
                        "2.手抓饼\n" +
                        "3.椒盐大虾");

        LANUCH_SUGGEST.put("星期二",lanuchSuggestList2);

        // 周三午餐
        List lanuchSuggestList3 = new ArrayList<>();
        lanuchSuggestList3.add(
                "今天午餐建议食谱:\n" +
                        "1.香米饭\n" +
                        "2.茄汁龙利鱼\n" +
                        "3.彩椒西蓝花\n" +
                        "4.麦当汤");
        lanuchSuggestList3.add(
                "今天午餐建议食谱:\n" +
                        "1.太极米饭\n" +
                        "2.红烧排骨\n" +
                        "3.手撕包菜\n" +
                        "4.西湖牛肉羹");
        LANUCH_SUGGEST.put("星期三",lanuchSuggestList3);

        // 周四午餐
        List lanuchSuggestList4 = new ArrayList<>();
        lanuchSuggestList4.add(
                "今天午餐建议食谱:\n" +
                        "1.老北京炸酱面\n" +
                        "2.蜜汁鸡腿\n" +
                        "3.水果糖");
        lanuchSuggestList4.add(
                "今天午餐建议食谱:\n" +
                        "1.牛肉担担面\n" +
                        "2.蜜烤翅根\n" +
                        "3.凤凰玉米羹");
        LANUCH_SUGGEST.put("星期四",lanuchSuggestList4);

        // 周五午餐
        List lanuchSuggestList5 = new ArrayList<>();
        lanuchSuggestList5.add(
                "今天午餐建议食谱:\n" +
                        "1.红薯米饭\n" +
                        "2.冬瓜炖鸡腿\n" +
                        "3.蒜薹茼蒿\n" +
                        "4.西湖牛肉面");
        lanuchSuggestList5.add(
                "今天午餐建议食谱:\n" +
                        "1.二米饭\n" +
                        "2.清蒸龙利鱼\n" +
                        "3.家常豆腐\n" +
                        "4.小吊梨汤");
        LANUCH_SUGGEST.put("星期五",lanuchSuggestList5);

        // 周六午餐
        List lanuchSuggestList6 = new ArrayList<>();
        lanuchSuggestList6.add(
                "今天午餐建议食谱:\n" +
                        "1.香米饭\n" +
                        "2.茄汁龙利鱼\n" +
                        "3.彩椒西蓝花\n" +
                        "4.麦当汤");
        lanuchSuggestList6.add(
                "今天午餐建议食谱:\n" +
                        "1.太极米饭\n" +
                        "2.红烧排骨\n" +
                        "3.手撕包菜\n" +
                        "4.西湖牛肉羹");
        LANUCH_SUGGEST.put("星期六",lanuchSuggestList6);

        // 周日午餐
        List lanuchSuggestList7 = new ArrayList<>();
        lanuchSuggestList7.add(
                "今天午餐建议食谱:\n" +
                        "1.老北京炸酱面\n" +
                        "2.蜜汁鸡腿\n" +
                        "3.水果糖");
        lanuchSuggestList7.add(
                "今天午餐建议食谱:\n" +
                        "1.牛肉担担面\n" +
                        "2.蜜烤翅根\n" +
                        "3.凤凰玉米羹");
        LANUCH_SUGGEST.put("星期日",lanuchSuggestList7);


        // 晚餐

        // 周一晚餐
        List dinnerSuggestList1 = new ArrayList<>();
        dinnerSuggestList1.add(
                "今天晚餐建议食谱:\n" +
                        "1.大米银耳粥\n" +
                        "2.芝麻酱卷\n" +
                        "3.木须肉\n" +
                        "4.清炒上海青\n");
        dinnerSuggestList1.add(
                "今天晚餐建议食谱:\n" +
                        "1.大米燕麦粥\n" +
                        "2.南瓜风车卷\n" +
                        "3.四喜烤麸\n" +
                        "4.蒜蓉西蓝花");
        DINNER_SUGGEST.put("星期一",dinnerSuggestList1);

        // 周二晚餐
        List dinnerSuggestList2 = new ArrayList<>();
        dinnerSuggestList2.add(
                "今天晚餐建议食谱:\n" +
                        "1.黑米粥\n" +
                        "2.豆沙卷\n" +
                        "3.彩椒牛腩\n" +
                        "4.豆豉油麦菜");
        dinnerSuggestList2.add(
                "今天晚餐建议食谱:\n" +
                        "1.白云蛋花羹\n" +
                        "2.荷叶夹\n" +
                        "3.京酱肉丝\n" +
                        "4.小炒藕丁");

        DINNER_SUGGEST.put("星期二",dinnerSuggestList2);

        // 周三晚餐
        List dinnerSuggestList3 = new ArrayList<>();
        dinnerSuggestList3.add(
                "今天晚餐建议食谱:\n" +
                        "1.白云蛋花羹\n" +
                        "2.荷叶夹\n" +
                        "3.京酱肉丝\n" +
                        "4.清炒笋丝");
        dinnerSuggestList1.add(
                "今天晚餐建议食谱:\n" +
                        "1.糯米蜜枣粥\n" +
                        "2.都画卷\n" +
                        "3.香菇肉片\n" +
                        "4.烧双茄");
        DINNER_SUGGEST.put("星期三",dinnerSuggestList3);

        // 周四晚餐
        List dinnerSuggestList4 = new ArrayList<>();
        dinnerSuggestList4.add(
                "今天晚餐建议食谱:\n" +
                        "1.香甜绿豆粥\n" +
                        "2.紫薯包\n" +
                        "3.海带焖肉丁\n" +
                        "4.荷塘月色");
        dinnerSuggestList4.add(
                "今天晚餐建议食谱:\n" +
                        "1.八宝粥\n" +
                        "2.葡萄干小圆馍\n" +
                        "3.蒜薹炒鸡丝\n" +
                        "4.糖醋娃娃菜");
        DINNER_SUGGEST.put("星期四",dinnerSuggestList4);

        // 周五晚餐
        List dinnerSuggestList5 = new ArrayList<>();
        dinnerSuggestList5.add(
                "今天晚餐建议食谱:\n" +
                        "1.麦仁香米粥\n" +
                        "2.花样馒头\n" +
                        "3.清炒空心菜\n" +
                        "4.香菇炒肉片");
        dinnerSuggestList5.add(
                "今天晚餐建议食谱:\n" +
                        "1.大米糙米粥\n" +
                        "2,玉米面发糕\n" +
                        "3.宫保鸡丁\n" +
                        "4.浇汁生菜");
        DINNER_SUGGEST.put("星期五",dinnerSuggestList5);

        // 周六晚餐
        List dinnerSuggestList6 = new ArrayList<>();
        dinnerSuggestList6.add(
                "今天晚餐建议食谱:\n" +
                        "1.白云蛋花羹\n" +
                        "2.荷叶夹\n" +
                        "3.京酱肉丝\n" +
                        "4.清炒笋丝");
        dinnerSuggestList6.add(
                "今天晚餐建议食谱:\n" +
                        "1.糯米蜜枣粥\n" +
                        "2.都画卷\n" +
                        "3.香菇肉片\n" +
                        "4.烧双茄");
        DINNER_SUGGEST.put("星期六",dinnerSuggestList6);

        // 周日晚餐
        List dinnerSuggestList7 = new ArrayList<>();
        dinnerSuggestList7.add(
                "今天晚餐建议食谱:\n" +
                        "1.香甜绿豆粥\n" +
                        "2.紫薯包\n" +
                        "3.海带焖肉丁\n" +
                        "4.荷塘月色");
        dinnerSuggestList7.add(
                "今天晚餐建议食谱:\n" +
                        "1.八宝粥\n" +
                        "2.葡萄干小圆馍\n" +
                        "3.蒜薹炒鸡丝\n" +
                        "4.糖醋娃娃菜");
        DINNER_SUGGEST.put("星期日",dinnerSuggestList7);


    }
}
